package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.City;
import com.internship.model.EnterpriseInfo;
import com.internship.model.JobInfo;
import com.internship.model.Province;
import com.internship.page.Page;
import com.internship.service.EnterpriseInfoService;


@Controller
@RequestMapping("enterpriseinfo")
public class EnterpriseInfoController {
	private final Logger logger = Logger.getLogger(EnterpriseInfoController.class);
	
	@Autowired
	private EnterpriseInfoService 	enterpriseinfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/enterpriseinfo_list.do")
	public ModelAndView getEnterpriseinfoList() {
		return getEnterpriseinfoList(0,0,null,1);
	}
	private List<Province>  provinceList = null;
	private List<City>  cityList = null;
	@RequestMapping("/enterpriseinfo_page_list.do")
	public ModelAndView getEnterpriseinfoList(@RequestParam int TheProvince,@RequestParam int TheCity,@RequestParam String Name,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("enterprise_info");
				Page<EnterpriseInfo> pageList = enterpriseinfoService.getEnterpriseinfoList(TheProvince, TheCity,Name, pageNumber);
				this.provinceList = enterpriseinfoService.getProvince();
				if(TheProvince>0){
					this.cityList = enterpriseinfoService.getCity(TheProvince);
				}else {
					this.cityList = enterpriseinfoService.getCity();
				}
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("provinceList",provinceList);
				mv.addObject("cityList",cityList);
				mv.addObject("Name",Name);
				mv.addObject("TheProvince",TheProvince);
				mv.addObject("TheCity",TheCity);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("enterpriseinfoList", pageList.getResultList());
		return mv;
	}
	@RequestMapping("/province.do")
	@ResponseBody
	public JSONObject province(@RequestParam("ProvinceId")int ProvinceId  ) {
		JSONObject json = new JSONObject();
		this.cityList = enterpriseinfoService.getCity(ProvinceId);
		json.put("cityList", cityList);
		return json;
	}
	/**?
	 * 添加实习单位
	 * @return
	 */
	@RequestMapping("/add_enterpriseinfo.do")
	public ModelAndView addEnterpriseinfo() {
		ModelAndView mv = new ModelAndView("enterprise_info_add");
		this.provinceList = enterpriseinfoService.getProvince();
		this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
		mv.addObject("provinceList",provinceList);
		mv.addObject("cityList",cityList);
		return mv;
	}
	@RequestMapping("/enterpriseinfo_save.do")
	@ResponseBody
	public JSONObject saveEnterpriseinfo(@RequestParam("Name")String  Name,@RequestParam("provinceId") int provinceId,@RequestParam("cityId") int cityId,@RequestParam("TheAddress") String TheAddress,@RequestParam("TheContact") String TheContact,@RequestParam("TheContactPhone") String TheContactPhone,@RequestParam("TheIntroduce") String TheIntroduce ) {
		JSONObject json = new JSONObject();
		EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
		enterpriseInfo.setName(Name);
		enterpriseInfo.setTheCity(cityId);
		enterpriseInfo.setTheProvince(provinceId);
		enterpriseInfo.setTheAddress(TheAddress);
		enterpriseInfo.setTheContact(TheContact);
		enterpriseInfo.setTheContactPhone(TheContactPhone);
		enterpriseInfo.setTheIntroduce(TheIntroduce);
		enterpriseInfo.setCreateType(0);
		int rel = enterpriseinfoService.addEnterpriseinfo(enterpriseInfo);
		json.put("result", rel>0?"success":"error");
		return json;
	}
	/**?
	 * 编辑实习单位
	 * @param Id
	 * @return
	 */
	@RequestMapping("/edit_enterpriseinfo.do")
	public ModelAndView editEnterpriseinfo(@RequestParam("Id")int  Id) {
		ModelAndView mv = new ModelAndView("enterprise_info_edit");
		this.provinceList = enterpriseinfoService.getProvince();
		this.cityList = enterpriseinfoService.getCity();
		mv.addObject("provinceList",provinceList);
		mv.addObject("cityList",cityList);
		EnterpriseInfo enterpriseInfo = enterpriseinfoService.getEnterpriseinfo(Id);
		mv.addObject("enterpriseInfo",enterpriseInfo);
		return mv;
	}
	@RequestMapping("/enterpriseinfo_update.do")
	@ResponseBody
	public JSONObject updateEnterpriseinfo(@RequestParam("Id")int  Id,@RequestParam("Name") String Name,@RequestParam("provinceId") int provinceId,@RequestParam("cityId") int cityId ,@RequestParam("TheAddress") String TheAddress,@RequestParam("TheContact") String TheContact,@RequestParam("TheContactPhone") String TheContactPhone,@RequestParam("TheIntroduce") String TheIntroduce) {
		JSONObject json = new JSONObject();
		try {
			EnterpriseInfo enterpriseInfo =enterpriseinfoService.getEnterpriseinfo(Id);
			enterpriseInfo.setName(Name);
			enterpriseInfo.setTheAddress(TheIntroduce);
			System.out.println(cityId+"==========");
			enterpriseInfo.setTheCity(cityId);
			enterpriseInfo.setTheContact(TheContact);
			enterpriseInfo.setTheContactPhone(TheContactPhone);
			enterpriseInfo.setTheIntroduce(TheIntroduce);
			enterpriseInfo.setTheProvince(provinceId);
			int rel  = enterpriseinfoService.updateEnterpriseinfo(enterpriseInfo);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delEnterpriseinfo(@RequestParam int Id){
			JSONObject json = new JSONObject();
			int  rel= enterpriseinfoService.delEnterpriseinfo(Id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
}